package cn.iocoder.yudao.module.biz.service.expand.targets.channel;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.biz.controller.admin.expand.targets.channel.vo.*;
import cn.iocoder.yudao.module.biz.dal.dataobject.expand.targets.channel.ExpandTargetChannelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.biz.dal.mysql.expand.targets.channel.ExpandTargetChannelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
import static cn.iocoder.yudao.module.biz.enums.ErrorCodeConstants.*;

/**
 * 拓展目标_渠道 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class ExpandTargetChannelServiceImpl implements ExpandTargetChannelService {

    @Resource
    private ExpandTargetChannelMapper expandTargetChannelMapper;

    @Override
    public Long createExpandTargetChannel(ExpandTargetChannelSaveReqVO createReqVO) {
        // 插入
        LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
        createReqVO.setOwnerUserId(loginUser.getId());
        createReqVO.setDeptId(Long.parseLong(loginUser.getInfo().get("deptId")));
        ExpandTargetChannelDO expandTargetChannel = BeanUtils.toBean(createReqVO, ExpandTargetChannelDO.class);
        expandTargetChannelMapper.insert(expandTargetChannel);

        // 返回
        return expandTargetChannel.getId();
    }

    @Override
    public void updateExpandTargetChannel(ExpandTargetChannelSaveReqVO updateReqVO) {
        // 校验存在
        validateExpandTargetChannelExists(updateReqVO.getId());
        // 更新
        ExpandTargetChannelDO updateObj = BeanUtils.toBean(updateReqVO, ExpandTargetChannelDO.class);
        expandTargetChannelMapper.updateById(updateObj);
    }

    @Override
    public void deleteExpandTargetChannel(Long id) {
        // 校验存在
        validateExpandTargetChannelExists(id);
        // 删除
        expandTargetChannelMapper.deleteById(id);
    }

    @Override
        public void deleteExpandTargetChannelListByIds(List<Long> ids) {
        // 删除
        expandTargetChannelMapper.deleteByIds(ids);
        }


    private void validateExpandTargetChannelExists(Long id) {
        if (expandTargetChannelMapper.selectById(id) == null) {
            throw exception(BIZ_EXPAND_TARGET_CHANNEL_NOT_EXISTS);
        }
    }

    @Override
    public ExpandTargetChannelDO getExpandTargetChannel(Long id) {
        return expandTargetChannelMapper.selectById(id);
    }

    @Override
    public PageResult<ExpandTargetChannelDO> getExpandTargetChannelPage(ExpandTargetChannelPageReqVO pageReqVO) {
        return expandTargetChannelMapper.selectPage(pageReqVO);
    }

    @Override
    public Integer checkExistByName(String name) {
        return expandTargetChannelMapper.checkExistByName(name);
    }
}